﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<div id="updateDialog" class="container-fluid">
    <div class="row">
        <form id="updateForm" class="form-horizontal" method="post">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="oldpassword">
                    <span class="help-inline"><font color="red">*</font> </span>
                    旧密码：</label>
                <div class="col-sm-5">
                    <input type="password" class="form-control" id="oldpassword" name="oldpassword" placeholder="旧密码" minlength="6" maxlength="20" value="" readonly/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="password">
                    <span class="help-inline"><font color="red">*</font> </span>
                    新密码：</label>
                <div class="col-sm-5">
                    <input type="password" class="form-control" id="password" name="password" placeholder="新密码" minlength="6" maxlength="20" value="" readonly/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="repeatpassword">
                    <span class="help-inline"><font color="red">*</font> </span>
                    确认新密码：</label>
                <div class="col-sm-5">
                    <input type="password" class="form-control" id="repeatpassword" name="repeatpassword" placeholder="确认新密码"  minlength="6"  maxlength="20" value=""/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-8 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="updatePasswordDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>

    function createSubmit() {
        $.ajax({dataType: 'json',
            type: 'post',
            url: '${basePath}/manage/user/updatemypassword/${user.userId}',
            data: $('#updateForm').serialize(),
            beforeSend: function() {
                if ($('#oldpassword').val() == '') {
                    $('#oldpassword').focus();
                    return false;
                }
                if ($('#password').val() == '') {
                    $('#password').focus();
                    return false;
                }
                if ($('#repeatpassword').val() == '') {
                    $('#repeatpassword').focus();
                    return false;
                }
                if ($('#password').val() != $('#repeatpassword').val() ) {
                    $('#repeatpassword').focus();
                    return false;
                }
            },
            success: function(result) {
                if (result.code != 1) {
                    if (result.data instanceof Array) {
                        $.each(result.data, function(index, value) {
                            $.confirm({
                                theme: 'dark',
                                animation: 'rotateX',
                                closeAnimation: 'rotateX',
                                title: false,
                                content: value.errorMsg,
                                buttons: {
                                    confirm: {
                                        text: '确认',
                                        btnClass: 'waves-effect waves-button waves-light'
                                    }
                                }
                            });
                        });
                    } else {
                        $.confirm({
                            theme: 'dark',
                            animation: 'rotateX',
                            closeAnimation: 'rotateX',
                            title: false,
                            content: result.data,
                            buttons: {
                                confirm: {
                                    text: '确认',
                                    btnClass: 'waves-effect waves-button waves-light'
                                }
                            }
                        });
                    }
                } else {
                    updatePasswordDialog.close();
                    infoDialog("操作成功");
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $.confirm({
                    theme: 'dark',
                    animation: 'rotateX',
                    closeAnimation: 'rotateX',
                    title: false,
                    content: textStatus,
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'waves-effect waves-button waves-light'
                        }
                    }
                });
            }
        });
    }
</script>